package com.itbaizhan.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*自己义异常处理
* 实现HandlerExceptionResolver接口*/
//注释了全局异常（GlobalException.java）再测方式
@Component  //放进spring容器中
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();
        //异常属于空异常
        if(e instanceof NullPointerException){
            mv.setViewName("error");//跳转到error.jsp页面
        }else {
            mv.setViewName("error2");
        }
        //直接添加数据
        mv.addObject("msg",e);
        return mv;
    }
}
